﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class AccessoryAdd : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlBelongto.BindEnum(EnumeratorHelper.GetFieldTexts(typeof(Enumerator.AccessoryReplacementBelongto)));

                if (Request.QueryString["RegisterID"] != null)
                {
                    hiddenRegisterID.Value = Request.QueryString["RegisterID"];
                    hiddenMaintainID.Value = Request.QueryString["MaintainID"];
                }

                MasterDataBll bll = new MasterDataBll();
                List<SysDictionaryEntity> list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessoryPaymentsObject);
                ddlPaymentsObject.DataSource = list;
                ddlPaymentsObject.DataTextField = "Name";
                ddlPaymentsObject.DataValueField = "ID";
                ddlPaymentsObject.DataBind();

                list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessoryPaymentType);
                ddlPaymentType.DataSource = list;
                ddlPaymentType.DataTextField = "Name";
                ddlPaymentType.DataValueField = "ID";
                ddlPaymentType.DataBind();


                list = bll.GetDictionaryListFromCache(Enumerator.DictionaryType.AccessorySource);
                ddlAccessoryResource.DataSource = list;
                ddlAccessoryResource.DataTextField = "Name";
                ddlAccessoryResource.DataValueField = "ID";
                ddlAccessoryResource.DataBind();
                ddlAccessoryResource.Items.Add(new ListItem("", "0"));            
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string stockID = "";
            if (txtAccessoryCode1.Text != "")
            {
                stockID = new StockBll().getStockid(txtAccessoryCode1.Text);
            }
            StockBll bll = new StockBll();
           
            WsmAccessoryReplacementEntity entity = new WsmAccessoryReplacementEntity();
            entity.ReplacementID = Guid.NewGuid();
            entity.MaintainID = new Guid(hiddenMaintainID.Value);
            entity.QuotePrice = string.IsNullOrEmpty(txtQuotePrice.Text.Trim()) ? 0 : Convert.ToDecimal(txtQuotePrice.Text.Trim());
            entity.BillOrderStatus = 0;//订单状态 0 未下订单 1 已下订单 2 入库
            entity.ReplacementNum = string.IsNullOrEmpty(txtReplacementNum.Text.Trim()) ? 0 : Convert.ToInt32(Convert.ToDecimal(txtReplacementNum.Text.Trim()));
            entity.ReplacementBelongto = Convert.ToInt32(ddlBelongto.SelectedValue);
            //if (ddlAccessoryResource.SelectedValue != "")
            //    entity.AccessoryResource = new Guid(ddlAccessoryResource.SelectedValue);//配件来源
            entity.AccessoryName = txtAccessaryName.Text;
            entity.AccessoryPaymentStatus = Convert.ToInt32(ddlAccessoryPaymentStatus.SelectedValue);
            entity.CheckMan = Guid.Empty;
            entity.CheckDate = DateTime.Now;
            if (stockID != "")
            {
                entity.StockID = new Guid(stockID);
            }
            entity.CheckStatus = (int)Enumerator.AccessoryReplacementCheckStatus.Saved;
            if (ddlPaymentsObject.SelectedValue != "")
                entity.PaymentsObject = new Guid(ddlPaymentsObject.SelectedValue);
            if (ddlPaymentType.SelectedValue != "")
                entity.PaymentType = new Guid(ddlPaymentType.SelectedValue);

            /////entity.StockID = "";
            entity.BillNum = "";
            entity.Deleted = false;
            entity.AccessoryID = new Guid(txtAccessaryID.Value);
            entity.PaymentsObjectDesc = txtPaymentsObjectDesc.Text;

            entity.PersonalStockQuantity = System.Convert.ToInt32(Convert.ToDecimal(txtStock.Text.Trim()));
            entity.AccssoryCode = txtAccssoryCode.Text;
            bll.Insert(entity);
            WsmAccessoryStockEntity stockEntity = new SystemBll().Get<WsmAccessoryStockEntity>(new Guid(stockID));
            if (txtStockStatus.Text == "个人")
            {
                stockEntity.AccessoryStatus = 2;                        
            }
            if (txtStockStatus.Text == "在库")
            {
                stockEntity.AccessoryStatus = 3;                                         
            }
            bll.Update(stockEntity);
                
            ClientScript.RegisterStartupScript(this.GetType(), "success", "SaveAndCloseWindow();", true);
        }

        protected void refreshbtn_Click(object sender, EventArgs e)
        {
            BindMyStockList(true);
        }

        private void BindMyStockList(Boolean needDataBind)
        {
            DataTable dt = new DataTable();
            if (txtAccessaryID.Value.Trim() != "")
            {
                dt = new CommonBll().getDataFromSQL("select b.*,'个人' as StockStatus, '1' as PersonalStockQuantity,a.AccessoryCode,a.StockID,a.StockSource  from Cms_Wsm_AccessoryStock a inner join Cms_Wsm_AccessoryInfo b on a.AccessoryID = b.AccessoryID and a.StockID in (select b.StockID from Cms_Wsm_AccessoryOut a inner join Cms_Wsm_StockInOut b on b.OutID = a.OutID where b.Receiver = '" + base.CurrentUserID.ToString() + "') and a.AccessoryID = '" + txtAccessaryID.Value.Trim() + "' union select b.*,'在库' as StockStatus, '1' as PersonalStockQuantity,a.AccessoryCode,a.StockID,a.StockSource  from Cms_Wsm_AccessoryStock a inner join Cms_Wsm_AccessoryInfo b on a.AccessoryID = b.AccessoryID where b.AccessoryID = '" + txtAccessaryID.Value.Trim() + "' and a.AccessoryStatus=0 order by AccessoryCode");
            }
            else
            {
                dt = new CommonBll().getDataFromSQL("select b.*,'个人' as StockStatus, '1' as PersonalStockQuantity,a.AccessoryCode,a.StockID,a.StockSource  from Cms_Wsm_AccessoryStock a inner join Cms_Wsm_AccessoryInfo b on a.AccessoryID = b.AccessoryID and a.AccessoryStatus=1 and a.StockID in (select b.StockID from Cms_Wsm_AccessoryOut a inner join Cms_Wsm_StockInOut b on b.OutID = a.OutID where b.OutQuantity=0 and b.Receiver = '" + base.CurrentUserID.ToString() + "') order by AccessoryCode");
            }

            gridAccessoryList.DataSource = dt;
            if (needDataBind)
            {
                gridAccessoryList.DataBind();
            }
        }
        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnSelect = e.Item.FindControl("lnkbtnSelect") as LinkButton;
                DataRowView dv = e.Item.DataItem as DataRowView;
                lnkbtnSelect.Attributes.Add("onclick", "SelectAccessaryRow(this,'" + dv["AccessoryID"].ToString() + "');return false;");
            }
        }

        protected void gridAccessoryList_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            BindMyStockList(false);
        }
    }
}
